package com.mostromas.ess.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.SysRole;
import com.mostromas.ess.system.mapper.SysRoleMapper;
import com.mostromas.ess.system.req.SysRoleREQ;
import com.mostromas.ess.system.service.ISysRoleService;
import com.mostromas.ess.util.base.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Override
    public Result queryPage(SysRoleREQ req) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper();
        // 条件查询
        if(StringUtils.isNotEmpty(req.getName())) {
            wrapper.like("name", req.getName());
        }
        wrapper.orderByAsc("update_date");
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }

    @Override
    public Result deleteById(String id) {
        // 1. 通过角色 id 删除角色信息表数据
        baseMapper.deleteById(id);
        // 2. 通过角色 id 删除角色菜单关系表数据
        baseMapper.deleteRoleMenuByRoleId(id);
        return Result.ok();
    }

    @Override
    public Result findMenuIdsById(String id) {
        return Result.ok(baseMapper.findMenuIdsById(id));
    }

    @Transactional
    @Override
    public Result saveRoleMenu(String roleId, List<String> menuIds) {
        //先清除该角色的权限值
        baseMapper.deleteRoleMenuByRoleId(roleId);
        //再将新的值保存
        if(CollectionUtils.isNotEmpty(menuIds)){
            baseMapper.saveRoleMenu(roleId,menuIds);
        }
        return Result.ok();
    }

    /*@Override
    public SysRole getById(Serializable id) {
        if(id == null){
            return new SysRole();
        }
        SysRole sysRole = super.getById(id);
        return null;
    }*/

}
